package top.saodisheng.config;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import top.saodisheng.pojo.User;

/**
 * @author saodisheng
 * @date 2021/8/19
 */
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
	@Override
	protected Class<?> getBeanClass(Element element) {
		return User.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
		setPropertyValue("id", element, builder);
		setPropertyValue("name", element, builder);
		setPropertyValue("city", element, builder);
	}

	private void setPropertyValue(String attributeName, Element element, BeanDefinitionBuilder builder) {
		String attributeValue = element.getAttribute(attributeName);
		if (StringUtils.hasText(attributeValue)) {
			// <property name="" value="" />
			builder.addPropertyValue(attributeName, attributeValue);
		}
	}
}
